var express = require("express");
var router = express.Router();
var { isPointInPolygon } = require("../Plugins/isPointInPolygon");

const { crowdModel } = require("../model/zq");
const { mzhModel } = require("../model/mzh");
router.get("/detect/:id", async (req, res) => {
  let _id = req.params.id;
  let body = req.body;
  let data = await crowdModel.updateOne({ _id }, { $set: body });
  res.send({
    data,
    code: 200,
  });
});
//判断危险区
router.post("/Inquire", async (req, res) => {
  let marks = req.body.markers;
  let newArray = [];
  for (let j = 0; j < marks.length; j++) {
    let obj = {};
    obj.lng = marks[j].longitude;
    obj.lat = marks[j].latitude;
    newArray.push(obj);
  }
  let corowd = await crowdModel.find();
  let list = [];
  corowd.forEach((item) => {
    let obj = {};
    obj.lat = item.latitude;
    obj.lng = item.longitude;
    let flag = isPointInPolygon(obj, newArray);
    if (flag == false) {
      list.push(item);
    }
  });
  let everyNum = (await mzhModel.find({ username: 'admin' }))[0].num
  res.send({
    code: 200,
    list,
    total: corowd.length,
    errornum: list.length
  });
});
//判断警告区
router.post("/warn", async (req, res) => {
  let marks = req.body.markers;
  let newArray = [];
  for (let j = 0; j < marks.length; j++) {
    let obj = {};
    obj.lng = marks[j].longitude;
    obj.lat = marks[j].latitude;
    newArray.push(obj);
  }
  let corowd = await crowdModel.find();
  let list = [];
  corowd.forEach((item) => {
    let obj = {};
    obj.lat = item.latitude;
    obj.lng = item.longitude;
    let flag = isPointInPolygon(obj, newArray);
    if (flag == false) {
      list.push(item);
    }
  });
 
  res.send({
    code: 200,
    list,
    total: corowd.length,
    errornum: list.length
  });
});
module.exports = router;
